﻿Public Class TMyCharacterSets
    Implements System.IDisposable
    Private _List As New List(Of TMyCharacterSet)
    Private myQuery As New TMyQuery

    Public Sub New(ByVal AConnection As TMySession)
        myQuery.Session = AConnection
        Init()
    End Sub
    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property

    Public ReadOnly Property Collations(ByVal ACharacterSetName As String) As List(Of TMyCollation)
        Get
            For i As Integer = 0 To _List.Count - 1
                If _List(i).CharacterSetName = ACharacterSetName Then
                    Return _List(i).Collations
                End If
            Next
            Return Nothing
        End Get
    End Property
    Default Public ReadOnly Property Items(ByVal Index As Integer) As TMyCharacterSet
        Get
            Return _List(Index)
        End Get
    End Property

    Private Sub Init()
        Dim item As TMyCharacterSet = Nothing
        Dim coll As TMyCollation = Nothing
        Dim added As Boolean = False
        _List.Clear()
        myQuery.Clear()
        myQuery.SQL.AppendLine("select")
        myQuery.SQL.AppendLine(" s.character_set_name")
        myQuery.SQL.AppendLine(",s.default_collate_name")
        myQuery.SQL.AppendLine(",s.description")
        myQuery.SQL.AppendLine(",s.maxlen")
        myQuery.SQL.AppendLine(",a.collation_name")
        myQuery.SQL.AppendLine(",c.id")
        myQuery.SQL.AppendLine(",c.is_default")
        myQuery.SQL.AppendLine(",c.is_compiled")
        myQuery.SQL.AppendLine(",c.sortlen")
        myQuery.SQL.AppendLine("from")
        myQuery.SQL.AppendLine(" information_schema.character_sets s")
        myQuery.SQL.AppendLine("left outer join information_schema.collation_character_set_applicability a on(s.character_set_name=a.character_set_name)")
        myQuery.SQL.AppendLine("left outer join information_schema.collations c on(a.collation_name=c.collation_name)")
        myQuery.SQL.AppendLine("order by")
        myQuery.SQL.AppendLine(" s.character_set_name")

        myQuery.Execute()
        While Not myQuery.Eof
            If item Is Nothing OrElse Not item.CharacterSetName.Equals(myQuery.FieldAsString("character_set_name")) Then
                item = New TMyCharacterSet
                item._CharacterSetName = myQuery.FieldAsString("character_set_name")
                item._DefaultCollateName = myQuery.FieldAsString("default_collate_name")
                item._Description = myQuery.FieldAsString("description")
                item._Maxlen = myQuery.FieldAsInteger("maxlen")
                added = False
            End If
            coll = New TMyCollation
            coll._CollationName = myQuery.FieldAsString("collation_name")
            coll._CharacterSetName = myQuery.FieldAsString("character_set_name")
            coll._ID = myQuery.FieldAsInteger("id")
            coll._IsDefault = myQuery.FieldAsString("is_default")
            coll._IsCompiled = myQuery.FieldAsString("is_compiled")
            coll._Sortlen = myQuery.FieldAsInteger("sort_len")
            item._List.Add(coll)
            If Not added Then
                _List.Add(item)
                added = True
            End If
            myQuery.MoveNext()
        End While
        myQuery.Close()
    End Sub

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには


    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            myQuery.Close()
            myQuery.Dispose()
            _List.Clear()
            _List = Nothing
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
